package com.angel.architecture.spring;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.angel.architecture.runner.ApplicationProcessRunner;
import com.angel.common.helpers.CollectionHelper;

/**
 *
 * @author William
 */
public class ApplicationContextSpring {
    private static ApplicationContextSpring INSTANCE;

    private static final String TX_PROXY_TEMPLATE_NAME = "txProxyTemplate";
    
    private static String[] contexts = new String[]
        {
            "classpath:applicationContext-model.xml",
            "classpath:applicationContext-dao.xml",
            "classpath:applicationContext-flex.xml",
            "classpath:applicationContext-services.xml"
        };

    private ApplicationContext applicationContext;

    private ApplicationContextSpring(String[] applicationContexts){
        super();
        if(applicationContext == null){
        	String[] allContexts = buildApplicationContextFilesWith(applicationContexts);
            applicationContext = new ClassPathXmlApplicationContext(allContexts);
        }
    }

    /**
     * Build an array with all spring configuration files. In this array are, architecture base configurations files, and
     * it adds others context configurations files.
     *
     * @param othersContexts others configurations files.
     * @return an string array with all configuration files names.
     */
    public static String[] buildApplicationContextFilesWith(String[] othersContexts){
    	List<String> ownContexts = CollectionHelper.convertGenericTo(contexts);
    	List<String> newContexts = CollectionHelper.convertGenericTo(othersContexts);
    	List<String> allContexts = CollectionHelper.union(ownContexts, newContexts);
    	String[] allArrayContexts = CollectionHelper.convertGenericTo(allContexts);
    	return allArrayContexts;
    }

    private ApplicationContextSpring(){
        super();
        if(applicationContext == null){
            applicationContext = new ClassPathXmlApplicationContext(contexts);
        }
    }

    public synchronized static ApplicationContextSpring createInstance(String[] applicationContext){
        if(INSTANCE == null){
            INSTANCE = new ApplicationContextSpring(applicationContext);
        }
        return INSTANCE;
    }

    public synchronized static ApplicationContextSpring createInstance(){
        if(INSTANCE == null){
            INSTANCE = new ApplicationContextSpring();
        }
        return INSTANCE;
    }

    public ApplicationContextSpring prepareApplicationContext(){
        return INSTANCE;
    }

	/**
	 * @return the applicationContext
	 */
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public ApplicationProcessRunner getApplicationProcessRunner() {
		return (ApplicationProcessRunner) this.getApplicationContext().getBean(TX_PROXY_TEMPLATE_NAME);
	}
}

